//
//  KeyboardControl.m
//  FinancialController
//
//  Created by Tan NguyenBa on 3/1/12.
//  Copyright (c) 2012 Home. All rights reserved.
//

#import "KeyboardControl.h"

@implementation KeyboardControl

/*************************************************************************************
 Function name: backToOriginalSize
 Function description: back to original size of scroll view when keyboard hide
 Input variables: 
 Output variables: N/A
 Note:
 Author: Tan Nguyen
 *************************************************************************************/
+ (void) backToOriginalSize:(BOOL *)keyboardVisible
                 withOffset:(CGPoint *)offset 
                 withScroll:(UIScrollView *)scrollview
                  withWidth:(CGFloat)widthScreen
                 withHeight:(CGFloat)heightScreen
{
	if (*keyboardVisible)
	{		
		// Reset the scrollview to previous location
		scrollview.contentOffset = *offset;
		scrollview.frame = CGRectMake(0, 0, widthScreen, heightScreen);		
		// Keyboard is no longer visible
		scrollview.scrollEnabled = YES;
		*keyboardVisible = NO;	
	}	
}

/*************************************************************************************
 Function name: scrollFrameForTextBox
 Function description: scroll view to display text field when keyboard show
 Input variables: 
 Output variables: N/A
 Note:
 Author: Tan Nguyen
 *************************************************************************************/
+ (void) scrollFrameForTextBox:(BOOL *)keyboardVisible 
					withOffset:(CGPoint *)offset 
                withScrollView:(UIScrollView *) scrollview 
                 withTextField:(UITextField *) activeField
              withNotifacation:(NSNotification *)notif
					withHeight:(CGFloat) heigth
{
	if (!*keyboardVisible)
	{
		// Get the size of the keyboard.
		NSDictionary *info = [notif userInfo];
		NSValue* aValue = [info valueForKey:UIKeyboardFrameBeginUserInfoKey];	
		
		CGSize keyboardSize = [aValue CGRectValue].size;	
		
		// Save the current location so we can restore when keyboard is dismissed
		*offset = scrollview.contentOffset;
		
		// Resize the scroll view to make room for the keyboard
		CGRect viewFrame = scrollview.frame;	
		
		viewFrame.size.height -= keyboardSize.height;
		scrollview.frame = viewFrame;	
		
		CGRect textFieldRect = [activeField frame];
		//textFieldRect = [activeField convertRect:textFieldRect toView:scrollview];
		textFieldRect.origin.y =  textFieldRect.origin.y + 20;		
		
		[scrollview scrollRectToVisible:textFieldRect animated:YES];
		scrollview.scrollEnabled = NO;
		// Keyboard is now visible
		*keyboardVisible = YES;	
	}
}


@end
